package com.admin.service

import com.admin.dao.AdminIndexDao
import com.data.annotation.NoargAnnotation
import com.data.annotation.OpenAnnotation
import com.data.bean.IndexAdmin
import com.data.entity.IndexAdminState
import com.data.util.CheckDataUtils
import com.data.util.DatabaseIdUtils
import com.data.util.FinalStringUtils
import com.data.util.ImageLookUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.multipart.commons.CommonsMultipartFile
import java.util.*

@Service
@OpenAnnotation
@NoargAnnotation
class AdminIndexService {

    @Autowired
    var adminIndexDao: AdminIndexDao? = null

    fun indexBannerListService(): List<IndexAdmin> = adminIndexDao!!.indexBannerListDao()

    fun showImageService(image: String): ByteArray? = ImageLookUtils.readImage(FinalStringUtils.INDEXIMAGEPAHT, image)

    /**
     * 删除失败
     *
     * @param id
     * @return
     */
    fun deleteBannerListService(id: String): Boolean {
        val indexAdmin = this.adminIndexDao!!.findbyidBannerListDao(id)
        if (indexAdmin != null && this.adminIndexDao!!.deleteBannerListDao(id) > 0) {
            //获取删除的图片的名称
            val bImage = indexAdmin.bimage
            val imageName = indexAdmin.image
            //组装图片的路径
            val path = arrayOf(FinalStringUtils.INDEXIMAGEPAHT + imageName, FinalStringUtils.INDEXIMAGEPAHT + bImage)
            //循环删除图片，这个过程可能会删除失败，但是数据库中已经删除，如果删除失败，文件就是垃圾文件。只能远程手工删除
            for (fileName in path) {
                ImageLookUtils.deleteImage(fileName)
            }
            return true
        }
        return false

    }

    fun changeStateService(id: String, state: Boolean): Boolean {
        if (CheckDataUtils.stringUtilsID(id)) {
            val indexAdminState = IndexAdminState(id = id, state = state, states = !state)
            return this.adminIndexDao!!.changeStateDao(indexAdminState) > 0
        }
        return false
    }

    /**
     * 服务层的保存图片
     *
     * @param image
     * @param bImage
     * @param title
     * @return
     */
    fun submitIndexBannerService(image: CommonsMultipartFile, bImage: CommonsMultipartFile, title: String, type: Int): Boolean {
        val isuffix = ImageLookUtils.getFileSuffix(image)
        val bsuffix = ImageLookUtils.getFileSuffix(bImage)
        if (CheckDataUtils.iconUtils(isuffix!!) && CheckDataUtils.iconUtils(bsuffix!!)) {
            val indexAdmin = IndexAdmin()
            //前景图的名称
            var path = DatabaseIdUtils.dataIdOne + isuffix
            indexAdmin.image = path//设置第一张图
            if (ImageLookUtils.saveFile(FinalStringUtils.INDEXIMAGEPAHT + path, image)) {//保存前景图
                path = DatabaseIdUtils.dataIdOne + bsuffix
                if (ImageLookUtils.saveFile(FinalStringUtils.INDEXIMAGEPAHT + path, bImage)) { //保存背景图
                    //保存到数据库中
                    indexAdmin.bimage = path//设置第二张图
                    indexAdmin.id = DatabaseIdUtils.dataIdOne
                    indexAdmin.title = title
                    indexAdmin.type = type
                    indexAdmin.date = Date()
                    return this.adminIndexDao!!.saveMassageDao(indexAdmin) > 0
                }
            }
        }
        return false
    }
}
